package com.evan.review.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.evan.review.R;

/**
 * 自定义布局的DialogFragment
 * 演示如何创建完全自定义的对话框界面
 */
public class CustomDialogFragment extends DialogFragment {

    private static final String ARG_MESSAGE = "message";
    
    private String message;
    private DialogListener listener;
    private EditText etInput;
    
    /**
     * 对话框回调接口
     */
    public interface DialogListener {
        void onDialogPositiveClick(CustomDialogFragment dialog, String inputText);
        void onDialogNegativeClick(CustomDialogFragment dialog);
    }
    
    /**
     * 创建实例的工厂方法
     */
    public static CustomDialogFragment newInstance(String message) {
        CustomDialogFragment fragment = new CustomDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_MESSAGE, message);
        fragment.setArguments(args);
        return fragment;
    }
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            message = getArguments().getString(ARG_MESSAGE, "");
        }
    }
    
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 确保Activity实现了接口
        if (context instanceof DialogListener) {
            listener = (DialogListener) context;
        } else {
            throw new ClassCastException(context.toString()
                    + " 必须实现DialogListener接口");
        }
    }
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 创建自定义视图
        View view = inflater.inflate(R.layout.fragment_custom_dialog, container, false);
        
        // 设置消息文本
        TextView tvMessage = view.findViewById(R.id.tv_message);
        tvMessage.setText(message);
        
        // 获取输入框
        etInput = view.findViewById(R.id.et_input);
        
        // 设置按钮点击事件
        Button btnPositive = view.findViewById(R.id.btn_positive);
        Button btnNegative = view.findViewById(R.id.btn_negative);
        
        btnPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDialogPositiveClick(CustomDialogFragment.this, 
                            etInput.getText().toString());
                }
                dismiss();
            }
        });
        
        btnNegative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDialogNegativeClick(CustomDialogFragment.this);
                }
                dismiss();
            }
        });
        
        return view;
    }
    
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
    
    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }
} 